const router = require('koa-router')()
const Record = require('../model/record')

router.prefix('/records')

router.get('/', async (ctx, next) => {
    let data = await Record.find({ uid: ctx.token.id })

    data = JSON.parse(JSON.stringify(data));

    data.forEach(val => {
        val.created_at = new Date(val.created_at).Format("yyyy-MM-dd hh:mm:ss")
        val.updated_at = new Date(val.updated_at).Format("yyyy-MM-dd hh:mm:ss")
        
        //console.log(val)
    })
    
    await ctx.render('record/index', {
        back_url: '/users',
        list: data
    })
})

router.get('/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var data = await Record.find({ tid: id })

    data = JSON.parse(JSON.stringify(data));

    data.forEach(val => {
        val.created_at = new Date(val.created_at).Format("yyyy-MM-dd hh:mm:ss")
        val.updated_at = new Date(val.updated_at).Format("yyyy-MM-dd hh:mm:ss")
        
        //console.log(val)
    })

    await ctx.render('record/index', {
        back_url: '/topik/' + id,
        list: data
    })
})

module.exports = router